<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
	:maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    :footer="null">
    <div v-if="!isUrl">
      <a-button v-print="'#printContent'" ghost type="primary">打印</a-button>
      <div ref="print" id="printContent" >
        <table style="width: 100%;margin-top: 50px">
          <tr>
            <td align="center"><h1>授权委托书</h1></td>
          </tr>
          <tr>
            <td style="font-size: 20px;text-indent:2em">
              本授权委托书声明：本单位法人代表<span style="text-decoration:underline"> {{faRen}} </span>现授权委托本单位的 <span style="text-decoration:underline"> {{weiTuoRen}} </span> 全权代理<span style="text-decoration:underline"> {{ contractName }} </span>-合同签订的一切相关事宜。<br>
              <div style="text-align: right">授权人：<img width="15%" v-if="faRenImg" :src="faRenImg"></div>
              <div style="text-align: right">被授权人：<img width="15%" :src="weiTuoRenImg" v-if="weiTuoRenImg"></div>
            </td>
          </tr>
          <tr style="height: 50px">
          </tr>
          <tr>
            <td align="right" style="font-size: 20px">
              <div style="margin-right: 50px">
                单位名称：<span> {{unit}} </span><br>
                {{ date }}
              </div>

            </td>
          </tr>
        </table>
      </div>
    </div>
    <iframe style="border: 0;width: 100%;height: 400px;" v-if="isUrl" :src="url">

    </iframe>

  </a-modal>
</template>

<script>
  import { httpAction,getAction,postAction } from '@/api/manage'
  import Vue from 'vue'
  import user from '@/store/modules/user'
  export default {
    name: 'PrintModal',
    data(){
      return{
        url:'',
        isUrl:false,
        date:this.dateFormat("YYYY年mm月dd日",new Date()),
        weiTuoRen:'',
        weiTuoRenImg:'',
        faRen:'',
        faRenImg:'',
        contractName:'',
        unit:'',
        title:"操作",
        visible: false,
        confirmLoading: false,
        tableCode: '',
        url: {
          getDetailByExpendType: '/base/printConfiguration/getDetailByExpendType',
          getPlanPrintByExpendType: '/base/printConfiguration/getPlanPrintByExpendType',
          getUserByName: '/sys/user/getUserByName'
        }
      }
    },
    methods: {
      getUserByNameMethod(frUserName,wtrUserName){
        if(frUserName){ //如果法人账号不为空，就通过法人账号查法人签名
          getAction(this.url.getUserByName,{userName: frUserName}).then(resFr => {
            if(resFr.success){
              if(resFr.result.sign){ //如果有值
                this.faRenImg = 'data:' + resFr.result.sign
              }
            }
          })
        }else{
          getAction(this.url.getUserByName,{userName: wtrUserName}).then(resFr => {
            if(resFr.success){
              if(resFr.result.sign){ //如果有值
                this.weiTuoRenImg = 'data:' + resFr.result.sign
              }
            }
          })
        }
      },
      dateFormat(fmt, date) {
        let ret;
        const opt = {
          "Y+": date.getFullYear().toString(),        // 年
          "m+": (date.getMonth() + 1).toString(),     // 月
          "d+": date.getDate().toString(),            // 日
          "H+": date.getHours().toString(),           // 时
          "M+": date.getMinutes().toString(),         // 分
          "S+": date.getSeconds().toString()          // 秒
          // 有其他格式化字符需求可以继续添加，必须转化成字符串
        };
        for (let k in opt) {
          ret = new RegExp("(" + k + ")").exec(fmt);
          if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
          };
        };
        return fmt;
      },
      show(id){
        this.isUrl=false
        this.visible=true
        let user = Vue.ls.get('Login_Userinfo')
        console.log(user)
        this.weiTuoRen=user.username
        this.getUserByNameMethod('',user.username);
        getAction("/ContractInfo/contractInfo/queryContractInfoById",{id:id}).then(res=>{
          if(res.success){
            let obj = res.result.contractInfo

            this.contractName=obj.contractName

            getAction("/sys/dict/getDictItems/ZXSBDW").then(resdict=>{
              if(resdict.success){
                if(resdict.result[0]){
                  this.unit=resdict.result[0].title
                }else{
                  this.unit='未配置单位'
                }
                getAction("/sys/dict/getDictText/FAREN/name").then(resfaren=>{
                  if(resfaren.success){
                    if(resfaren.result){
                      this.faRen=resfaren.result
                    }else{
                      this.faRen='未配置法人'
                    }
                    this.save(id)
                    this.getUserByNameMethod(resfaren.result,'');
                  }
                })
              }
            })


          }
        })
      },


      handleOk(){

      },
      showUrl(id,url){
        this.isUrl=true
        this.visible=true
        let powerOfAttorney = {}
        powerOfAttorney.contractId=id
        let user = Vue.ls.get('Login_Userinfo')
        this.weiTuoRen=user.username
        getAction("/ContractInfo/contractInfo/queryContractInfoById",{id:id}).then(res=>{
          if(res.success){
            let obj = res.result.contractInfo
            this.contractName=obj.contractName
            getAction("/sys/dict/getDictItems/ZXSBDW").then(resdict=>{
              if(resdict.success){
                if(resdict.result[0]){
                  this.unit=resdict.result[0].title
                }else{
                  this.unit='未配置单位'
                }
                getAction("/sys/dict/getDictText/FAREN/name").then(resfaren=>{
                  if(resfaren.success){
                    if(resfaren.result){
                      this.faRen=resfaren.result
                    }else{
                      this.faRen='未配置法人'
                    }
                    powerOfAttorney.legalPerson=this.faRen
                    powerOfAttorney.person=this.weiTuoRen
                    powerOfAttorney.contractTitle=this.contractName
                    powerOfAttorney.unitName=this.unit
                    postAction("/powerOfAttorney/powerOfAttorney/queryAndAdd",powerOfAttorney).then(res=>{
                      this.url = url
                    })
                  }
                })
              }
            })


          }
        })
      },
      save(id){
        let powerOfAttorney = {}
        powerOfAttorney.contractId=id

        let user = Vue.ls.get('Login_Userinfo')

        this.weiTuoRen=user.username
        getAction("/ContractInfo/contractInfo/queryContractInfoById",{id:id}).then(res=>{
          if(res.success){
            let obj = res.result.contractInfo

            this.contractName=obj.contractName

            getAction("/sys/dict/getDictItems/ZXSBDW").then(resdict=>{
              if(resdict.success){
                if(resdict.result[0]){
                  this.unit=resdict.result[0].title
                }else{
                  this.unit='未配置单位'
                }
                getAction("/sys/dict/getDictText/FAREN/name").then(resfaren=>{
                  if(resfaren.success){
                    if(resfaren.result){
                      this.faRen=resfaren.result
                    }else{
                      this.faRen='未配置法人'
                    }
                    powerOfAttorney.legalPerson=this.faRen
                    powerOfAttorney.person=this.weiTuoRen
                    powerOfAttorney.contractTitle=this.contractName
                    powerOfAttorney.unitName=this.unit
                    postAction("/powerOfAttorney/powerOfAttorney/queryAndAdd",powerOfAttorney).then(res=>{
                    })
                  }
                })
              }
            })


          }
        })



      },
      showedUrl(cid,url){
        this.isUrl=true
        getAction("/powerOfAttorney/powerOfAttorney/queryByCid",{id:cid}).then(res=>{
          if(res.success && res.result){
            this.visible=true
            this.url = url
          }else{
            this.$message.warn("此合同没有生成授权委托书！")
          }
        })
      },
      showeUrl(url){
        this.isUrl=true
        this.visible=true
        this.url = url
      },
      showed(cid){
        this.isUrl=false
        getAction("/powerOfAttorney/powerOfAttorney/queryByCid",{id:cid}).then(res=>{
          if(res.success && res.result){
            this.visible=true
            this.faRen=res.result.legalPerson
            this.getUserByNameMethod(res.result.legalPerson,'')
            this.weiTuoRen=res.result.person
            this.getUserByNameMethod('',res.result.person)
            this.contractName=res.result.contractTitle
            this.unit=res.result.unitName
            this.date=this.dateFormat("YYYY年mm月dd日",res.result.createTime)
          }else{
            this.$message.warn("此合同没有生成授权委托书！")
          }
        })
      },
      handleCancel(){
        this.close()
      },
      close(){
        this.tableCode = ''
        this.visible = false
        this.faRen = ''
        this.weiTuoRen = ''
        this.contractName = ''
        this.unit = ''
      }
    }



  }
</script>

<style scoped>

</style>